package action.main;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import option.OperationEnum;
import service.BookService;
import service.CategoryService;
import entity.Category;

/**
 *主页面servlet
 */
@SuppressWarnings("serial")
public class MainServlet extends HttpServlet{
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String action = (String) request.getAttribute("action");
		if (action.equals(OperationEnum.MAIN_TOMAIN.getValue())) {//跳转到主页面
			request.setAttribute("reload", "reload");
			
			//左栏分类
			CategoryService categoryService = new CategoryService();
			//编辑推荐
			BookService bookService = new BookService();
			try {
				request.setAttribute("categoryList", categoryService.queryAllCategory());
				request.setAttribute("recommendBooks", bookService.queryRecommendBooks());
				request.setAttribute("hotBooks", bookService.queryHotBooks());
				request.setAttribute("newBooks", bookService.queryNewBooks());
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			request.getRequestDispatcher("/WEB-INF/pages/main/main.jsp").forward(request, response);
		} else if (action.equals(OperationEnum.MAIN_BOOKLIST.getValue())) {//跳转到图书列表页面
			Integer parent_id = Integer.parseInt(request.getParameter("parent_id"));
			Integer sub_id = Integer.parseInt(request.getParameter("sub_id"));
			int page = request.getParameter("page") == null ? 1 : Integer.parseInt(request.getParameter("page"));
			String order = request.getParameter("order") == null ? "add_time" : request.getParameter("order");
			int pageSize = 3;//默认每页显示数量
			long totalCount = 0L;
			long totalPageCount = 0L;
			
			List<Map<String, Object>> categoryList = null;
			List<Map<String, Object>> bookList = null;
			Category parentCategory = null;
			
			//分类
			CategoryService categoryService = new CategoryService();
			BookService bookService = new BookService();
			try {
				//book_list.jsp左栏信息
				categoryList = categoryService.querySubCategoryInfo(parent_id);
				parentCategory = categoryService.queryCategoryById(parent_id);
				//总条目数
				for (int i = 0; i < categoryList.size(); i++) {
					totalCount += (Long)categoryList.get(i).get("count");
				}
				
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("cat_id", sub_id);
				map.put("page", page);
				map.put("pageSize", pageSize);
				map.put("order", order);
				bookList = bookService.queryBookByCid(map);
				
				long total = bookService.queryTotalSizeByCid(sub_id);
				totalPageCount = total % pageSize == 0 ? total / pageSize : total / pageSize + 1;
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				request.setAttribute("categoryList", categoryList);
				request.setAttribute("parentCategory", parentCategory);
				request.setAttribute("totalCount", totalCount);
				request.setAttribute("bookList", bookList);
				request.setAttribute("page", page);
				request.setAttribute("order", order);
				request.setAttribute("totalPageCount", totalPageCount);
				request.setAttribute("parent_id", parent_id);
				request.setAttribute("sub_id", sub_id);
			}
			
			request.getRequestDispatcher("/WEB-INF/pages/main/book_list.jsp").forward(request, response);
		}
	}
}
